﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repository;

namespace GameSchoolApplication.Controllers
{ 
	[Authorize]
    public class AnswerController : Controller
    {
        private GameSchoolWebContext _db = new GameSchoolWebContext();
        private TaskRepository _task = new TaskRepository();
        
        // GET: /Answer/
        public ViewResult Index()
        {
            var answers = _db.Answers.Include(a => a.Question);
            return View(answers.ToList());
        }

        // GET: /Answer/Details/5
        public ViewResult Details(int id)
        {
            Answer answer = _db.Answers.Find(id);
            return View(answer);
        }

        // GET: /Answer/Create
        [HttpGet]
        public ActionResult Create(int questionId)
        {
            Question question = _task.GetQuestion(questionId);
            return View(new Answer { Question = question, QuestionId = questionId });
        } 

        // POST: /Answer/Create
        [HttpPost]
        public ActionResult Create(Answer answer)
        {
            if (ModelState.IsValid)
            {
                _task.AddAnswer(answer);
                return RedirectToAction("Details", "Question", new { id = answer.QuestionId });
            }
            return View(answer);            
        }
        
        // GET: /Answer/Edit/5
        [HttpGet]
        public ActionResult Edit(int id)
        {
            Answer answer = _db.Answers.Find(id);
            ViewBag.QuestionId = new SelectList(_db.Questions, "QuestionId", "Content", answer.QuestionId);
            return View(answer);
        }

        // POST: /Answer/Edit/5
        [HttpPost]
        public ActionResult Edit(Answer answer)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(answer).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.QuestionId = new SelectList(_db.Questions, "QuestionId", "Content", answer.QuestionId);
            return View(answer);
        }

        // GET: /Answer/Delete/5
        [HttpGet]
        public ActionResult Delete(int id)
        {
            Answer answer = _db.Answers.Find(id);
            return View(answer);
        }

        // POST: /Answer/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Answer answer = _db.Answers.Find(id);
            _db.Answers.Remove(answer);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}